﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Vds2465Telegrams;

namespace Vds2465Parser
{
	[TestClass]
	public class Generator_FixedBytes_A31_Test
	{
		#region tests

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram01()
		{
			Telegram_Calling telegram = CreateA31_Telegram01();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x49, 0x01, 0x4A, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram02()
		{
			Telegram_Returning telegram = CreateA31_Telegram02();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x2B, 0x01, 0x2C, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram03()
		{
			Telegram_Calling telegram = CreateA31_Telegram03();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x7A, 0x01, 0x7B, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram04()
		{
			Telegram_Returning telegram = CreateA31_Telegram04();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x68, 0x09, 0x09, 0x68, 0x28, 0x01, 0x05, 0x02, 0x11, 0x07, 0x05, 0x01, 0x10, 0x5E, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram05()
		{
			Telegram_Calling telegram = CreateA31_Telegram05();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x5A, 0x01, 0x5B, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram06()
		{
			Telegram_Calling telegram = CreateA31_Telegram06();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x68, 0x0D, 0x0D, 0x68, 0xF3, 0x01, 0x05, 0x02, 0x11, 0x07, 0x05, 0x01, 0x10, 0x02, 0x56, 0x87, 0x09, 0x11, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram07()
		{
			Telegram_Returning telegram = CreateA31_Telegram07();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x09, 0x01, 0x0A, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram08()
		{
			Telegram_Returning telegram = CreateA31_Telegram08();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x68, 0x09, 0x09, 0x68, 0x00, 0x01, 0x05, 0x03, 0x11, 0x07, 0x05, 0x01, 0x10, 0x37, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram09()
		{
			Telegram_Calling telegram = CreateA31_Telegram09();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x68, 0x09, 0x09, 0x68, 0x73, 0x01, 0x05, 0x03, 0x11, 0x07, 0x05, 0x01, 0x10, 0xAA, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test_A31_Telegram10()
		{
			Telegram_Returning telegram = CreateA31_Telegram10();

			IGeneratorParser generatorParser = Helpers.GetGeneratorParser();
			byte[] telegramBytes = generatorParser.GenerateTelegramBytes(telegram);

			byte[] expectedBytes = new byte[] { 0x10, 0x00, 0x01, 0x01, 0x16 };
			Assert.IsTrue(Helpers.Equals2ByteArrays(telegramBytes, expectedBytes));
		}

		#endregion tests

		#region static helpers

		private static Telegram_Calling CreateA31_Telegram01()
		{
			const byte address = 0x01;

			Telegram_Calling telegram = new Telegram_Calling(eDirection.RCT_to_ACE, address, eTelegramFunction_Calling.REQ_STATUS, false, false);

			return telegram;
		}

		private static Telegram_Returning CreateA31_Telegram02()
		{
			const byte address = 0x01;

			Telegram_Returning telegram = new Telegram_Returning(eDirection.RCT_to_ACE, address, eTelegramFunction_Returning.RESPOND_STATUS, true, false);

			return telegram;
		}

		private static Telegram_Calling CreateA31_Telegram03()
		{
			const byte address = 0x01;

			Telegram_Calling telegram = new Telegram_Calling(eDirection.RCT_to_ACE, address, eTelegramFunction_Calling.REQ_NDAT, true, true);

			return telegram;
		}

		private static Telegram_Returning_RESPOND_NDAT CreateA31_Telegram04()
		{
			ReportType_Fire reportType = new ReportType_Fire(true, eReportSubType_Fire.General);
			UserData_02_StatusChangeReport2Confirm userData02 = new UserData_02_StatusChangeReport2Confirm(0x11, 0x07, 0x05, eAddressExtension_1.Input, reportType);
			UserDataColl userDataColl = new UserDataColl();
			userDataColl.Add(userData02);

			const byte address = 0x01;
			Telegram_Returning_RESPOND_NDAT telegram = new Telegram_Returning_RESPOND_NDAT(eDirection.RCT_to_ACE, address, true, false, userDataColl);

			return telegram;
		}

		private static Telegram_Calling CreateA31_Telegram05()
		{
			const byte address = 0x01;

			Telegram_Calling telegram = new Telegram_Calling(eDirection.RCT_to_ACE, address, eTelegramFunction_Calling.REQ_NDAT, true, false);

			return telegram;
		}

		private static Telegram_Calling_SEND_NDAT CreateA31_Telegram06()
		{
			ReportType_Fire reportType = new ReportType_Fire(true, eReportSubType_Fire.General);
			UserData_02_StatusChangeReport2Confirm userData02 = new UserData_02_StatusChangeReport2Confirm(0x11, 0x07, 0x05, eAddressExtension_1.Input, reportType);
			UserDataColl userDataColl = new UserDataColl();
			userDataColl.Add(userData02);

			UserData_56_IdentificationNumber userData56 = new UserData_56_IdentificationNumber("7890");
			userDataColl.Add(userData56);

			Telegram_Calling_SEND_NDAT telegram = new Telegram_Calling_SEND_NDAT(eDirection.ATE_to_RCT, 0x01, true, true, userDataColl);

			return telegram;
		}

		private static Telegram_Returning CreateA31_Telegram07()
		{
			const byte address = 0x01;

			Telegram_Returning telegram = new Telegram_Returning(eDirection.RCT_to_ACE, address, eTelegramFunction_Returning.RESPOND_NAK, false, false);

			return telegram;
		}

		private static Telegram_Returning_CONFIRM_ACK CreateA31_Telegram08()
		{
			ReportType_Fire reportType = new ReportType_Fire(true, eReportSubType_Fire.General);
			UserData_03_StatusChangeConfirm userData03 = new UserData_03_StatusChangeConfirm(0x11, 0x07, 0x05, eAddressExtension_1.Input, reportType);
			UserDataColl userDataColl = new UserDataColl();
			userDataColl.Add(userData03);

			const byte address = 0x01;
			Telegram_Returning_CONFIRM_ACK telegram = new Telegram_Returning_CONFIRM_ACK(eDirection.RCT_to_ACE, address, false, false, userDataColl);

			return telegram;
		}

		private static Telegram_Calling_SEND_NDAT CreateA31_Telegram09()
		{
			ReportType_Fire reportType = new ReportType_Fire(true, eReportSubType_Fire.General);
			UserData_03_StatusChangeConfirm userData03 = new UserData_03_StatusChangeConfirm(0x11, 0x07, 0x05, eAddressExtension_1.Input, reportType);
			UserDataColl userDataColl = new UserDataColl();
			userDataColl.Add(userData03);

			Telegram_Calling_SEND_NDAT telegram = new Telegram_Calling_SEND_NDAT(eDirection.RCT_to_ACE, 0x01, true, true, userDataColl);

			return telegram;
		}

		private static Telegram_Returning CreateA31_Telegram10()
		{			
			Telegram_Returning telegram = new Telegram_Returning(eDirection.RCT_to_ACE, 0x01, eTelegramFunction_Returning.CONFIRM_ACK, false, false);
			
			return telegram;
		}

		#endregion static helpers
	}
}
